<!--登录日志-->
<template>
  <div id="loginLog" class="app-container">
    <!--搜索条件-->
    <div class="filter-container">
      <el-form :model="listQuery" :inline="true">
        <el-form-item label="状态">
          <el-select v-model="listQuery.isSuccess" placeholder="请选择状态" @change="handleFilter">
            <el-option label="全部" value="" />
            <el-option label="登录成功" value="1" />
            <el-option label="登录失败" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="listQuery.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="真实姓名">
          <el-input v-model="listQuery.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="IP">
          <el-input v-model="listQuery.loginIp" placeholder="请输入登录IP地址" />
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="dateTime" type="datetimerange" value-format="timestamp" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" plain @click="handleFilter">搜索</el-button>
          <el-button icon="el-icon-refresh" plain @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table-container flex-main">
      <div class="table-container-header">
        <table-toolbar :create="false" @refresh="handleFilter" @remove="removeRecord">
          <el-button plain icon="el-icon-brush" @click="clearRecord()">全部清空</el-button>
        </table-toolbar>
      </div>
      <el-table
        ref="multipleTable"
        :data="loginLogs"
        header-cell-class-name="table-header"
        fit
        height="100%"
        highlight-current-row
        @row-click="handleRowClick"
        @selection-change="handleSelectionChange"
        @sort-change="sortChange">
        <el-table-column type="selection" sortable width="50" />
        <el-table-column align="center" prop="id" label="#" width="60">
          <template slot-scope="scope">{{ scope.$index+1 }}</template>
        </el-table-column>
        <el-table-column label="状态" prop="isSuccess" align="center" width="70">
          <template slot-scope="scope">
            <span v-if="scope.row.isSuccess==1" class="color-primary">成功</span>
            <span v-if="scope.row.isSuccess==0" class="color-danger">失败</span>
          </template>
        </el-table-column>
        <el-table-column prop="gmtCreated" sortable label="登录时间" width="150">
          <template slot-scope="scope">{{ scope.row.gmtCreated | formatDate }}</template>
        </el-table-column>
        <el-table-column label="用户名" prop="userName" align="center" min-width="140" />
        <el-table-column label="真实姓名" prop="realName" align="center" min-width="140" />
        <el-table-column label="登录方式" prop="loginType" align="center" width="100" />
        <el-table-column label="登录地" prop="loginAddress" align="left" min-width="280">
          <template slot-scope="scope">
            <span v-if="scope.row.loginAddress"> {{ scope.row.loginAddress }}</span>
            <span v-if="scope.row.loginIp" class="color-primary"> ({{ scope.row.loginIp }})</span>
          </template>
        </el-table-column>
        <el-table-column label="浏览器" prop="brower" min-width="190" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.browser }}<span v-if="scope.row.browser" class="color-primary"> ({{ scope.row.browserVersion }})</span>
          </template>
        </el-table-column>
        <el-table-column label="操作系统" prop="os" min-width="140" />

        <el-table-column label="操作描述" prop="remark" min-width="200" show-overflow-tooltip />
      </el-table>
    </div>
    <div class="pagination_handler">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />
    </div>
  </div>
</template>
<script>
export default {

  components: { },

  data() {
    return {
      loginLogs: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        sort: this.common.SORT_DEFAULT,
        userName: '',
        realName: '',
        loginIp: '',
        isSuccess: ''
      },
      dateTime: [],

      multipleSelectionRecords: []
    }
  },
  watch: {

  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      const response = await this.$store.dispatch('userLoginHistoryPage', this.listQuery)
      this.loginLogs = response.records
      this.total = response.total
    },
    /**
     * 全选/反选
     */
    handleSelectionChange(val) {
      this.multipleSelectionRecords = val
    },
    // 固定写法
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        if (order === 'ascending') {
          this.listQuery.sort = '+' + prop
        } else {
          this.listQuery.sort = '-' + prop
        }
      } else {
        this.listQuery.sort = this.common.SORT_DEFAULT
      }
      this.handleFilter()
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      if (this.dateTime.length > 0) {
        this.listQuery.startTime = this.dateTime[0]
        this.listQuery.endTime = this.dateTime[1]
      }
      this.fetchData()
    },
    handleRowClick(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    /**
     * 重置
     */
    handleReset() {
      this.dateTime = []
      this.listQuery = {
        ...this.listQuery,
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        sort: this.common.SORT_DEFAULT,
        userName: '',
        realName: '',
        loginIp: '',
        isSuccess: '',
        startTime: '',
        endTime: ''
      }
      this.handleFilter()
    },
    handleRemoveSingle(row) {
      this.multipleSelectionRecords = []
      this.multipleSelectionRecords.push(row)
      this.$refs.multipleTable.clearSelection()
      this.$refs.multipleTable.toggleRowSelection(row, true)
      this.removeRecord()
    },
    removeRecord() {
      if (this.multipleSelectionRecords.length > 0) {
        const ids = []
        this.multipleSelectionRecords.forEach(function(v, i) {
          ids.push(v.id)
        })
        this.$confirm('此操作将永久删除记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('userLoginHistoryRemove', ids).then(response => {
            this.handleFilter()
            this.$modal.msgSuccess('删除成功')
          }).catch(() => {})
        }).catch(() => {
        })
      } else {
        this.$modal.msgWarning('请选择要删除的记录')
      }
    },
    clearRecord() {
      this.$confirm('此操作将清空所有数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('userLoginHistorClear').then(response => {
          this.handleFilter()
          this.$modal.msgSuccess('清空完成')
        }).catch(() => {})
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" >

</style>
